package handler.server;

import com.alibaba.fastjson.JSONObject;
import handler.ChannelCach;
import handler.dataPackage.DriverDeviceMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;

/**
 * @author dfj@99chuxing.com
 * @date 2017/12/13 0013 16:43
 */
public class ServerHandler extends ChannelInboundHandlerAdapter{

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("读取数据-----------");
        DriverDeviceMessage DriverDeviceMessage =(DriverDeviceMessage) msg;
        DriverDeviceMessage driverDeviceMessage1=new DriverDeviceMessage();
        driverDeviceMessage1.setDeviceId("22221111");
        driverDeviceMessage1.setProtoType("Tcp");
        driverDeviceMessage1.setData("这是服务端返回的数据-------");
        String driverMessageString = JSONObject.toJSONString(driverDeviceMessage1);
        ctx.writeAndFlush(driverMessageString);

    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        /** 出现异常 ，打印关闭通道*/
        cause.printStackTrace();
        ctx.close();
    }

}
